var app = getApp()

let api = require("../../utils/api.js")
Page({
  data:{
    num: 1,
    totalNum: 0,
    hasCarts: false,
    curIndex: 0,
    show: false,
    scaleCart: false,
    goodId:'',
    orderId:'',
    // 商品数据
    goods: {
    code:200,
    msg:"成功",
    data:{
      id: 1,
      image: 'https://tgi12.jia.com/129/143/29143946.jpg',
      title: '精品装修',
      price: 1200,
      stock: '有货',
      detail: '3天完成装修',
      parameter: '90平米',
      service: '不支持退货'
    }
  }
  },

  addCount() {
    let num = this.data.num;
    num++;
    this.setData({
      num : num
    })
  },
  // 订单生成
  makeOrder:function (goodId) {
    var token = wx.getStorageSync('token')
    wx.request({
      url:api.ProjectOrderApi,
      data: {num:this.data.num,good_id:this.data.goodId,'price':this.data.goods.data.price},
      dataType: "json",
      header: {'Authorization':'JWT'+' '+token},
      method: "post",
      success: (result) => {
          // orderId 赋值
          this.setData({
            orderId:result.data.order_id
          });
        // 跳转页面
        let orderid = result.data.order_id
        wx.navigateTo({
          url: '/pages/endpay/endpay?Id=' + goodId+'&orderId=' + orderid,
        })
      },
    })

  },
// 支付
  addToPay(e) {
    // 进行支付
    // 跳转页面
    // console.log(e)
    let goodId =e.target.dataset.id
    // console.log(goodId)
    // 生成订单
    // console.log('生成订单')
      this.makeOrder(goodId)
  },

  bindTap(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      curIndex: index
    })
  },
  // 获取商品信息
  getGoodDetail:function (params) {
    var token = wx.getStorageSync('token')
    wx.request({
      url:api.IndexDetailContentApi+this.data.goodId+'/',
      dataType: "json",
      header: {'Authorization':'JWT'+' '+token},
      method: "get",
      success: (result) => {
        // console.log(result.data,'>>>>')
        if (result.data.code!=200){
          wx.showToast({
            title: '请登陆',
            icon:"error"
          })
          return
        }else{
          // 下单操作
          this.setData({
            goods:result.data
          })
        }
        
      },
    })
  },

  // 生命周期
  onLoad: function (options) {
    //console.log(options) // {id: "2"}
   let Id = options.Id;
    // console.log(Id)
    // console.log(Id,"获取商品信息，进行数据替换")
    // 保存商品id
    this.setData({
      goodId:Id
    })
    this.getGoodDetail()

 },
})